/* 
 *
 * Author: Allan <qimingos_lsk@163.com>, (C) 2005-2007
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */
#include "include/ShowLyric.h"
#include "include/EditLyricDlg.h"

#define this  theApp.m_LyricWnd.m_EditLyricDlg

void EditLyricDlgInit()
{
	this.m_bModfiy = FALSE;
	this.m_bEditing = FALSE;
	this.RefreshState = RefreshState;
	this.EditLyric = EditLyric;

	GtkWidget * pEditLyricDlg = glade_xml_get_widget(this.m_pEditLyricXML, "EditLyricDlg");

	XML_CONNECT_SIGNAL2(this.m_pEditLyricXML, on_EditLyricDlg_delete_event, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_MenuFileApply_activate, on_Apply, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_MenuFileDiscard_activate, on_Discard, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_MenuEditCut_activate, on_Cut, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_MenuEditCopy_activate, on_Copy, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_MenuEditPaste_activate, on_Paste, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_MenuEditDel_activate, on_Del, pEditLyricDlg);

	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_ToolbarApply_clicked, on_Apply, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_ToolbarReload_clicked, on_Discard, pEditLyricDlg);

	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_ToolbarCut_clicked, on_Cut, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_ToolbarCopy_clicked, on_Copy, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_ToolbarPaste_clicked, on_Paste, pEditLyricDlg);
	XML_CONNECT_SIGNAL(this.m_pEditLyricXML, on_ToolbarDel_clicked, on_Del, pEditLyricDlg);

	GtkWidget* pEditLyric =
			glade_xml_get_widget(this.m_pEditLyricXML, "TextViewLyric");
	GtkTextBuffer* pEditBuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
	gtk_text_buffer_create_tag(pEditBuf, "lyric", "foreground","green",NULL);

	g_signal_connect(GTK_OBJECT(pEditBuf), "changed",
			GTK_SIGNAL_FUNC(on_TestViewLyric_changed), NULL);
	g_signal_connect(GTK_OBJECT(pEditBuf), "mark-set",
			GTK_SIGNAL_FUNC(on_TestViewLyric_selected), NULL);
	
}

// 编辑歌词
void EditLyric()
{
	strncpy(this.m_szLyricFile, theApp.m_CurrSongInfo.szLyricFileName, MAX_PATH);
	if (strlen(this.m_szLyricFile) <= 0)
		return;

	GtkWidget * pEditLyricDlg = glade_xml_get_widget(this.m_pEditLyricXML, "EditLyricDlg");

	// 从文件中读取歌词内容
	FILE* file = fopen(this.m_szLyricFile, "r");
	if (!file)
	{
		LyricLog("%s:%d==>open file(%s) failed!", __FILE__, __LINE__,
		this.m_szLyricFile);
		return;
	}
	fseek(file, 0, SEEK_END);
	int iLen = ftell(file);
	if (iLen <= 0) return;
	fseek(file, 0, SEEK_SET);

	gchar* lpszLyric = (gchar *)malloc(iLen*sizeof(gchar));
	if (!lpszLyric)
	{
		LyricLog("%s:%d==>Not enough memory.", __FILE__, __LINE__);
		return;
	}
	GtkWidget* pEditLyric = glade_xml_get_widget(this.m_pEditLyricXML, "TextViewLyric");

	GtkTextBuffer* pEditBuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
	gtk_text_buffer_set_text(pEditBuf, "", -1);
	GtkTextIter end;
	while(fgets(lpszLyric, iLen, file) != NULL)
	{
		int iLineLen = strlen(lpszLyric);
		char* lpszTimeEnd = lpszLyric + iLineLen;
		if(iLineLen>2)
		{
			do
			{
				lpszTimeEnd--;
			}while(*lpszTimeEnd != ']' && lpszTimeEnd> lpszLyric);
			lpszTimeEnd++;
			int iEnd = lpszTimeEnd - lpszLyric;
			gtk_text_buffer_get_end_iter(pEditBuf, &end);
			gtk_text_buffer_insert_with_tags_by_name(pEditBuf, &end, lpszLyric, iEnd, "time", NULL);
		}
		gtk_text_buffer_get_end_iter(pEditBuf, &end);
		gtk_text_buffer_insert_with_tags_by_name(pEditBuf, &end, lpszTimeEnd, -1, "lyric", NULL);
	}
	fclose(file);
	file = NULL;
	free(lpszLyric);
	lpszLyric = NULL;
	
	char szTitle[MAX_PATH];
	snprintf(szTitle, MAX_PATH, "%s - %s", PLUGIN_NAME, this.m_szLyricFile);
	gtk_window_set_title(GTK_WINDOW(pEditLyricDlg), szTitle);
	this.m_bModfiy = FALSE;
	this.RefreshState();
	this.m_bEditing = TRUE;
	theApp.m_LyricWnd.SmartShowWnd(FALSE);
}
// 处理关闭事件
gboolean on_EditLyricDlg_delete_event(GtkWidget* widget, gpointer user_data)
{
	this.m_bEditing = FALSE;
	this.m_bModfiy = FALSE;
	theApp.m_LyricWnd.SmartShowWnd(TRUE);
	return TRUE;
}

// 保存歌词文件
void on_Apply(GtkWidget* widget, gpointer user_data)
{
	GtkWidget* pEditLyric = glade_xml_get_widget(
	this.m_pEditLyricXML,
	"TextViewLyric");
	GtkTextBuffer* pEditBuf =
			gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
	GtkTextIter start, end;
	/*获得缓冲区开始和结束位置的Iter*/
	gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(pEditBuf), &start, &end);
	/*获得文本框缓冲区文本*/
	gchar *lpszLyric = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(pEditBuf),
			&start, &end, FALSE);
	if (!lpszLyric)
	{
		LyricLog("%s:%d==>Can't get lyric from the view.",__FILE__ , __LINE__);
		return;
	}

	// 歌词内容写入文件
	FILE* file = fopen(this.m_szLyricFile, "w+");
	if (!file)
	{
		LyricLog("%s:%d==>open file(%s) failed!", __FILE__, __LINE__,
		this.m_szLyricFile);
		return;
	}
	fwrite(lpszLyric, strlen(lpszLyric), 1, file);
	fclose(file);
	file = NULL;

	// 显示歌词界面重新加载歌词
	SongInfo info;
	if (theApp.m_player.GetSongInfo(&info))
	{
		gchar szLrcFile[MAX_PATH] =
		{	0};
		sprintf(szLrcFile, "%s/%s/%s.lyric", theApp.m_configs.szLyricPath,
		info.szArtist, info.szTitle);
		strcpy(info.szLyricFileName, szLrcFile);
		theApp.PreChangeSong(&info);
	}
	this.m_bModfiy = FALSE;
	this.m_bEditing = FALSE;

	theApp.m_LyricWnd.SmartShowWnd(TRUE);
}

// 重新加载歌词
void on_Discard(GtkWidget* widget, gpointer user_data)
{
	LyricDebug("on_Discard");
	EditLyric();
	this.m_bModfiy = FALSE;
	RefreshState();
}

// 剪切
void on_Cut(GtkWidget* widget, gpointer user_data)
{
	GtkWidget* pEditLyric = glade_xml_get_widget(
	this.m_pEditLyricXML,
	"TextViewLyric");

	GtkTextBuffer* pEditBuf =
			gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
	GtkClipboard* clipboard = gtk_widget_get_clipboard(pEditLyric,
			GDK_SELECTION_CLIPBOARD);
	gtk_text_buffer_cut_clipboard(pEditBuf, clipboard, TRUE);
	// TODO:重新校正颜色
	this.m_bModfiy = TRUE;
	RefreshState();
}
// 拷贝
void on_Copy(GtkWidget* widget, gpointer user_data)
{
	GtkWidget* pEditLyric = glade_xml_get_widget(
	this.m_pEditLyricXML,
	"TextViewLyric");

	GtkTextBuffer* pEditBuf =
			gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
	GtkClipboard* clipboard = gtk_widget_get_clipboard(pEditLyric,
			GDK_SELECTION_CLIPBOARD);
	gtk_text_buffer_copy_clipboard(pEditBuf, clipboard);
	// TODO:重新校正颜色
	RefreshState();
}
// 粘贴
void on_Paste(GtkWidget* widget, gpointer user_data)
{
	GtkWidget* pEditLyric = glade_xml_get_widget(
	this.m_pEditLyricXML,
	"TextViewLyric");

	GtkTextBuffer* pEditBuf =
			gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
	GtkClipboard* clipboard = gtk_widget_get_clipboard(pEditLyric,
			GDK_SELECTION_CLIPBOARD);
	gtk_text_buffer_paste_clipboard(pEditBuf, clipboard, NULL, TRUE);

	// TODO:重新校正颜色

	this.m_bModfiy = TRUE;
	RefreshState();
}
// 删除
void on_Del(GtkWidget* widget, gpointer user_data)
{
	GtkWidget* pEditLyric = glade_xml_get_widget(
	this.m_pEditLyricXML,
	"TextViewLyric");

	GtkTextBuffer* pEditBuf =
			gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
	gtk_text_buffer_delete_selection(pEditBuf, FALSE, TRUE);
	this.m_bModfiy = TRUE;
	// TODO:重新校正颜色
	RefreshState();
}
void on_TestViewLyric_selected(GtkTextBuffer *textbuffer,
		GtkTextIter *location, GtkTextMark *mark, gpointer user_data)
{
	static gboolean bSel;
	if(bSel == gtk_text_buffer_get_has_selection(textbuffer)) return;
	bSel = !bSel;
	RefreshState();
}
void on_TestViewLyric_changed(GtkTextBuffer *textbuffer, gpointer user_data)
{
	if(!this.m_bModfiy)
	{
		this.m_bModfiy = TRUE;
		RefreshState();
	}
}
void RefreshState()
{
	GtkWidget * pEditLyricDlg = glade_xml_get_widget(this.m_pEditLyricXML, "EditLyricDlg");
	char szTitle[MAX_PATH];
	if (this.m_bModfiy)
	{
		snprintf(szTitle, MAX_PATH, "*%s - %s", 
		PLUGIN_NAME, 
		this.m_szLyricFile);
	}
	else
	{
		snprintf(szTitle, MAX_PATH, "%s - %s", 
		PLUGIN_NAME, 
		this.m_szLyricFile);
	}
	gtk_window_set_title(GTK_WINDOW(pEditLyricDlg), szTitle);

	GtkWidget* pEditLyric = glade_xml_get_widget(
	this.m_pEditLyricXML,
	"TextViewLyric");
	GtkTextBuffer* pEditBuf =
			gtk_text_view_get_buffer(GTK_TEXT_VIEW(pEditLyric));
#define ENABLE_WIDGET(widget, bEnable) gtk_widget_set_sensitive(\
				glade_xml_get_widget(this.m_pEditLyricXML, widget),\
				bEnable)
	if (gtk_text_buffer_get_has_selection(pEditBuf) == TRUE)
	{
		ENABLE_WIDGET("MenuEditCut", TRUE);
		ENABLE_WIDGET("MenuEditCopy", TRUE);
		ENABLE_WIDGET("MenuEditDel", TRUE);
		ENABLE_WIDGET("ToolbarCut", TRUE);
		ENABLE_WIDGET("ToolbarCopy", TRUE);
		ENABLE_WIDGET("ToolbarDel", TRUE);
	}
	else
	{
		ENABLE_WIDGET("MenuEditCut", FALSE);
		ENABLE_WIDGET("MenuEditCopy", FALSE);
		ENABLE_WIDGET("MenuEditDel", FALSE);
		ENABLE_WIDGET("ToolbarCut", FALSE);
		ENABLE_WIDGET("ToolbarCopy", FALSE);
		ENABLE_WIDGET("ToolbarDel", FALSE);
	}

	ENABLE_WIDGET("MenuFileApply", this.m_bModfiy);
	ENABLE_WIDGET("MenuFileDiscard", this.m_bModfiy);
	//ENABLE_WIDGET("ToolbarReload", this.m_bModfiy);
	ENABLE_WIDGET("ToolbarApply", this.m_bModfiy);

	// 检测剪贴板的状态
	GtkClipboard* clipboard = gtk_widget_get_clipboard(pEditLyric,
			GDK_SELECTION_CLIPBOARD);
	if (gtk_clipboard_wait_is_text_available(clipboard))
	{
		ENABLE_WIDGET("MenuEditPaste", TRUE);
		ENABLE_WIDGET("ToolbarPaste", TRUE);
	}
	else
	{
		ENABLE_WIDGET("MenuEditPaste", FALSE);
		ENABLE_WIDGET("ToolbarPaste", FALSE);
	}

}
